dubbo之token令牌 您所在的位置:网站首页 dubbo consumer调用provider步骤 dubbo之token令牌

dubbo之token令牌

2023-07-18 16:37| 来源: 网络整理| 查看: 265

1:写在前面

服务提供者作为服务的提供方,为了安全性,接口是不可以随意调用的,为了解决这个问题,dubbo提供了token令牌机制,只需要在service配置中设置token即可,下面我们来一起看下。

2:服务提供方 2.1:服务接口 public interface MyProviderTokenService { String sayHi(String word); } 2.2:服务接口实现 public class MyProviderTokenServiceImpl implements MyProviderTokenService { @Override public String sayHi(String word) { return "provider token: " + word; } } 2.3:xml

注意在中设置了token="12345678",即令牌为固定值,也可以设置token="true",此时dubbo会通过UUID的方式随机生成一个token。

2.4:main public class ProviderWithTokenMain { public static void main(String[] args) throws Exception { /* dubbo://192.168.10.119:20827/dongshi.daddy.service.MyProviderTokenService?anyhost=true&application=dongshidaddy-provider&bean.name=dongshi.daddy.service.MyProviderTokenService&dubbo=2.0.2&generic=false&interface=dongshi.daddy.service.MyProviderTokenService&methods=sayHi&owner=dongshidaddy&pid=17848&side=provider×tamp=1638325411607&token=12345678 */ //加载xml配置文件启动 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/provider-with-token.xml"); context.start(); System.in.read(); // 按任意键退出 } }

启动后生成的dubbo url如下:

dubbo://...?anyhost=true&application=dongshidaddy-provider&...&token=12345678

可以看到生成了&token=12345678参数。

3:服务消费方 3.1:xml

注意此时设置了连接注册中心。

3.2:main public class ConsumerWithTokenMain { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer-with-token.xml"); context.start(); MyProviderTokenService myProviderTokenService = (MyProviderTokenService) context.getBean("myProviderTokenServiceInConsumerSide"); System.out.println(myProviderTokenService.sayHi("hello")); System.in.read(); } }

调用正常。因为这种方式是通过注册中心的,token信息会自动带到服务提供者端,所以可以正常调用,下面我们看下通过api config的方式绕过注册中心的情况。

3.3:api配置绕过注册中心 public static void main(String[] args) { // 当前应用配置 ApplicationConfig application = new ApplicationConfig(); application.setName("dongshidaddy-consumer"); application.setOwner("dongshidaddy"); // 连接注册中心配置 RegistryConfig registry = new RegistryConfig(); // registry.setAddress("zookeeper://192.168.10.119:2181"); registry.setAddress("N/A"); // 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接 // 引用远程服务 ReferenceConfig reference = new ReferenceConfig(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏 reference.setApplication(application); reference.setRegistry(registry); // 多个注册中心可以用setRegistries() reference.setInterface(MyProviderTokenService.class); reference.setUrl("dubbo://192.168.10.119:20827"); // 和本地bean一样使用xxxService MyProviderTokenService myProviderTokenService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用 String s = myProviderTokenService.sayHi("hello dubbo1"); System.out.println(s); }

运行输出,无效的token相关错误,如下:

在这里插入图片描述

下面我们修改代码增加token的配置,如下:

在这里插入图片描述

再次调用,就正常了。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有